# Build recipe for libx86emu.
#
# Copyright (c) 2018 Markus Tornow, <tornow@riseup.net>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=libx86emu
version=2.1
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/libs"

tarname=${version}.tar.gz

# Remote source(s)
fetch=http://github.com/wfeldt/libx86emu/archive/"$tarname"

description="
ibx86emu is a small library to emulate x86 instructions.


The focus here is not a complete emulation (go for qemu for this) but to cover enough for typical firmware blobs.
At the moment regular 32-bit instructions are covered together with basic protected mode support.
Not done are fpu, mmx, or any of the other instruction set extensions.

The library lets you
• intercept any memory access or directly map real memory ranges
• intercept any i/o access, map real i/o ports, or block any real i/o
• intercept any interrupt
• provides hook to run after each instruction
• recognizes a special x86 instruction that can trigger logging

• has integrated logging to 
  - trace code execution, including register content and decoded instruction
  - trace memory and i/o accesses
  -provide statistics about accessed memory locations, i/o ports, and interrupts 
"

homepage=http://github.com/wfeldt/libx86emu
license="BSD style"

# Source documentation
docs="LICENSE LICENSE_INFO README.md"
docsdir="${docdir}/${program}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # disable regeneration of changlog and VERSION
    chmod -x ./git2log
    echo "${version}" > VERSION

    # set CFLAGS
#    sed -i "s/-02/$QICLAGS/" Makefile
        
    make \
        LIBDIR=/usr/lib${libSuffix}
 
    make -j${jobs} \
	DESTDIR="$destdir" \
        LIBDIR=/usr/lib${libSuffix} \
	install


    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}/"
}


